/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aeronef.builder;

import aeronef.appStates.GameState;
import aeronef.model.Gun;
import com.jme3.material.Material;
import com.jme3.scene.Spatial;

/**
 *
 * @author Hagan
 */
public class GunBuilder extends AbstractBuilder {

    protected ProjectileBuilder projectileBuilder;
    
    public GunBuilder(GameState gameState, ProjectileBuilder projectileBuilder) {
        super(gameState);
        this.projectileBuilder = projectileBuilder;
    }

    public Gun buildFromDefinition(String name, String type) {

        String definitionString = (String) assetManager.loadAsset("Data/ships/guns/" + type + ".xml");
        definition = buildDocument(definitionString);
        
        Gun gun = new Gun(stateManager, projectileBuilder);
        buildGunProperties(gun);
        buildTurretGeometry(gun);
        
        return gun;
    }

    private void buildGunProperties(Gun gun) {
       
       Float muzzleVelocity = Float.parseFloat(getTagValue("muzzleVelocity", definition.getDocumentElement()));
       gun.setUserData("muzzleVelocity", muzzleVelocity);
    }
    
    private void buildTurretGeometry(Gun gun) {
        
        String mesh = getTagValue("mesh", definition.getDocumentElement());
        Spatial gunGeo = assetManager.loadModel("Models/ships/guns/" + mesh + ".j3o");
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md"); // default material
        gunGeo.setMaterial(mat);
        gun.attachChild(gunGeo);
    }

}
